package com.blb.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blb.entity.*;
import com.blb.entity.page.BlbPage;
import com.blb.entity.page.DataTablePage;
import com.blb.service.*;
import com.blb.utils.ResponseHelp;
import com.blb.utils.ShiroUtils;
import org.apache.ibatis.javassist.expr.NewArray;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ZhengSainan
 * @since 2019-11-21
 */
@Controller
@RequestMapping("/warning")
public class WarningController {
    @Resource
    private  IWarningService warningService;
    @Autowired
    private IWareService wareService;
    @Autowired
    private ICargoAreaService cargoAreaService;
    @Autowired
    private ICargoService cargoService;
    @Autowired
    private IShelvesService shelvesService;
    /**
     * 跳转页面
     * @return
     */
    @RequiresPermissions("warning")
    @GetMapping("stockEarlyWarning")
    public String stockEarlyWarning(Model model){
        String companyId = ShiroUtils.getUser().getCompanyId();
        List<String> wares = warningService.getWare(companyId);
        model.addAttribute("wares",wares);
        List<Map<String, Object>> products = warningService.getProduct(companyId);
        model.addAttribute("products",products);
        List<Map<String, Object>> wareIds = warningService.getWareId(companyId);
        model.addAttribute("wareIds",wareIds);
        return "/warehouse/stock_early_warning";

    }

    /**
     * 分页数据
     * @param start
     * @param length
     * @param ware
     * @param product
     * @return
     */
    @RequiresPermissions("warning")
    @ResponseBody
    @RequestMapping("page")
    public BlbPage page(int start,int length,String ware,String product){
        String companyId = ShiroUtils.getUser().getCompanyId();
        //查询分页数据
        List<Map<String, Object>> warningMessage = warningService.getWarningMessage(start, length, ware, product,companyId);
        //查询条数
        int count = warningService.warningMessageCount(ware, product,companyId);
        return new DataTablePage(count,count,warningMessage);
    }

    /**
     * 计算条数
     * @param ware
     * @param product
     * @return
     */
    @RequiresPermissions("warning")
    @ResponseBody
    @RequestMapping("count")
    public ResponseHelp count(String ware, String product){
        String companyId = ShiroUtils.getUser().getCompanyId();
        ResponseHelp responseHelp = new ResponseHelp();
        int i = warningService.warningMessageCount(ware, product, companyId);
        responseHelp.setData(i);
        return responseHelp;
    }

    @RequiresPermissions("warning")
    @ResponseBody
    @RequestMapping("updateWarningNum")
    public ResponseHelp updateWarningNum(Warning warning){
        warningService.updateWarningNum(warning);
        return new ResponseHelp();
    }

    @ResponseBody
    @RequestMapping("addWarning")
    public ResponseHelp addWarning(Warning warning){
        HashMap<String , Object> m1 = new HashMap<>();
        m1.put("product_id",warning.getProductId());
        m1.put("warehouse_id",warning.getWarehouseId());
        Collection<Warning> warnings = warningService.listByMap(m1);
        if (warnings.size()==0){
            warningService.save(warning);
        }else {
            Warning w = new Warning();
            w.setWarningNum(warning.getWarningNum());
            warningService.update(w,new UpdateWrapper<Warning>().eq("product_id",warning.getProductId()).eq("warehouse_id",warning.getWarehouseId()));
        }
        return new ResponseHelp();
    }

}

